<?php

	include_once '../com/mss/model/Drug.php';
	include '../com/mss/common/Connection.php';

	class DrugAccessor {
	
		public function saveAll($drugs) {
			foreach ($drugs as $drug) {
				$this->save($drug);
			}
		}
	
		public function save(Drug $drug) {
			$connection = Connection::open();
	
			$name = $drug->getName();
			$serial = $drug->getSerial();
			$unit = $drug->getUnit();
			$description = $drug->getDescription();
	
			$query = "INSERT INTO SS_DRUG (NAME, SERIAL, UNIT, DESCRIPTION) VALUES ('$name', '$serial', '$unit', '$description')";
	
			mysql_query($query, $connection) or die("could not save data");
	
			Connection::close($connection);
		}
	
		public function selectAll() {
			$connection = Connection::open();
	
			$query = "SELECT ID, NAME, SERIAL, UNIT, DESCRIPTION FROM SS_DRUG";
	
			$result = mysql_query($query, $connection) or die("could not retrieve data");
	
			$drugs = array();
			while ($row = mysql_fetch_assoc($result)) {
				$id = $row['ID'];
				$name = $row['NAME'];
				$serial = $row['SERIAL'];
				$unit = $row['UNIT'];
				$description = $row['DESCRIPTION'];
	
				$drug = new Drug($id, $name, $serial, $unit, $description);
				array_push($drugs, $drug);
			}
	
			Connection::close($connection);
	
			return $drugs;
		}
	}

?>